/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pigx.admin.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.MechanismChaosGovernDto;
import com.pig4cloud.pigx.admin.api.entity.MechanismChaosGovern;
import com.pig4cloud.pigx.admin.api.entity.MechanismDept;
import com.pig4cloud.pigx.admin.api.enums.GovernStatusEnum;
import com.pig4cloud.pigx.admin.api.vo.ChaosGovernStatisticsVo;
import com.pig4cloud.pigx.admin.mapper.MechanismChaosGovernMapper;
import com.pig4cloud.pigx.admin.mapper.MechanismDeptMapper;
import com.pig4cloud.pigx.admin.service.MechanismChaosGovernService;
import com.pig4cloud.pigx.admin.service.MechanismDeptService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 四乱整治 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-20
 */
@Service
@AllArgsConstructor
public class MechanismChaosGovernServiceImpl extends ServiceImpl<MechanismChaosGovernMapper, MechanismChaosGovern> implements MechanismChaosGovernService {


    private final MechanismChaosGovernMapper mapper;


    /**
     * 查询四乱整治统计
     *
     * @param res
     * @return
     */
    @Override
    public ChaosGovernStatisticsVo queryStatistics(MechanismChaosGovernDto res) {
        List<ChaosGovernStatisticsVo.ChaosGovernStatisticsType> statisticsTypeList = mapper.queryStatistics(res);
        LambdaQueryWrapper<MechanismChaosGovern> wrappers = Wrappers.<MechanismChaosGovern>query().lambda()
                .lt(StringUtils.isNotEmpty(res.getEndTime()), MechanismChaosGovern::getGovernTime, res.getEndTime())
                .gt(StringUtils.isNotEmpty(res.getStartTime()), MechanismChaosGovern::getGovernTime, res.getStartTime());
        Long sumCount = mapper.selectCount(wrappers);
        wrappers.ne(MechanismChaosGovern::getStatus, GovernStatusEnum.GOVERNED.getType());
        Long unGovernSum = mapper.selectCount(wrappers);
        return ChaosGovernStatisticsVo.builder()
                .sum(sumCount.intValue())
                .notRectified(unGovernSum.intValue())
                .typeRatioList(statisticsTypeList)
                .build();
    }

    /**
     *
     * @param res
     * @return List<MechanismChaosGovern>
     * @author lfx
     * @date 2024/8/7 14:49
     * @Desc 查询四乱整治列表
     **/
    @Override
    public List<MechanismChaosGovern> selectList(MechanismChaosGovernDto res) {
        LambdaQueryWrapper<MechanismChaosGovern> wrappers = Wrappers.<MechanismChaosGovern>query().lambda()
                .lt(StringUtils.isNotEmpty(res.getEndTime()), MechanismChaosGovern::getGovernTime, res.getEndTime())
                .gt(StringUtils.isNotEmpty(res.getStartTime()), MechanismChaosGovern::getGovernTime, res.getStartTime())
                .eq(StringUtils.isNotEmpty(res.getType()), MechanismChaosGovern::getType, res.getType());
        List<MechanismChaosGovern> mechanismChaosGoverns = mapper.selectList(wrappers);
        return mechanismChaosGoverns;
    }
}
